package com.yoku.view.controller;

import com.dubbo.annotation.Consumer;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.eventbus.EventBus;
import com.yoku.mybatis.model.PageData;
import com.yoku.mybatis.model.Result;
import com.yoku.user.model.User;
import com.yoku.user.service.UserReadService;
import com.yoku.view.event.TestEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.UUID;

/**
 * Desc:
 * Date: 2016/12/3
 */

@RequestMapping(value = "/api/user")
@RestController
@Slf4j
public class UserController {

    @Consumer
    UserReadService userReadService;

    private final EventBus eventBus;

    @Autowired
    public UserController(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public User login(@RequestParam String username, @RequestParam String password){
        eventBus.post(new TestEvent("==============="));
        Result<User> userResult = userReadService.login(username, password);

        return null;
    }

    @RequestMapping(value = "/create")
    public Boolean create(){
        User user = new User();
        user.setUsername(UUID.randomUUID().toString().substring(0, 5));
        user.setPassword("222");
        user.setType(1);
        return userReadService.create(user);
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageData<User> page(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        Map<String, Object> param = Maps.newHashMap();
        param.put("type", 1);
        PageData<User> userPageData = userReadService.page(pageNo, pageSize, param);
        return userPageData;
    }
}
